/*
 */

package com.railway.files.action;

import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import cn.org.rapid_framework.page.Page;

import com.opensymphony.xwork2.ModelDriven;
import com.opensymphony.xwork2.Preparable;
import com.railway.common.base.BaseStruts2Action;
import com.railway.common.constants.SysConstant;
import com.railway.common.util.Json;
import com.railway.common.util.StaticMap;
import com.railway.news.model.Category;
import com.railway.news.model.CategoryUserLink;
import com.railway.news.service.ICategoryManager;
import com.railway.news.vo.query.CategoryQuery;

/**
 * @author jiangcheng
 * @version 1.0
 * @since 1.0
 */

public class FilesCategoryAction extends BaseStruts2Action implements Preparable,ModelDriven {

	protected static final String DEFAULT_SORT_COLUMNS = null;

	protected static final String LIST_JSP = "/admin/files/category/list.jsp";
	protected static final String CREATE_JSP = "/admin/files/category/create.jsp";
	protected static final String EDIT_JSP = "/admin/files/category/edit.jsp";
	protected static final String SHOW_JSP = "/admin/files/category/show.jsp";
	// redirect paths,startWith: !
	protected static final String LIST_ACTION = "!/files/FilesCategory/list.do";

	private ICategoryManager categoryManager;

	private String returnUrl;
	private Category category;
	java.lang.String id = null;
	private String[] items;
	private String param;

	public void setParam(String param) {
		this.param = param;
	}

	public void prepare() throws Exception {
		if (isNullOrEmptyString(id)) {
			category = new Category();
		} else {
			category = (Category)categoryManager.getById(id);
		}
	}
	/** 增加setXXXX()方法,spring就可以通过autowire自动设置对象属性,注意大小写 */
	public void setCategoryManager(ICategoryManager manager) {
		this.categoryManager = manager;
	}
	public Object getModel() {
		return category;
	}

	public void setId(java.lang.String val) {
		this.id = val;
	}
	public void setItems(String[] items) {
		this.items = items;
	}
	/** 执行搜索 */
	public String list() {
		return LIST_JSP;
	}
	public String listAjax() {
		CategoryQuery query = newQuery(CategoryQuery.class,DEFAULT_SORT_COLUMNS);
		//只查询类型是软件管理的分类 
		query.setCaTypes(SysConstant.CATEGORY_TYPE_SOFTWARE_MANAGEMENT);
		
		query.setSortColumns("CREATE_TIME");
		Page page = categoryManager.findPage(query);
		
		Map map = new HashMap();
		map.put("total", page.getTotalCount());
		map.put("rows", page.getResult());
		return writeAjaxResponse(Json.toJson(map));
	}

	/** 查看对象 */
	public String show() {
		return SHOW_JSP;
	}

	/** 进入新增页面 */
	public String create() {
		return CREATE_JSP;
	}

	/** 保存新增对象 */
	public String save() {
		
		//设置分类类型
		category.setCaTypes(SysConstant.CATEGORY_TYPE_SOFTWARE_MANAGEMENT);
		Date date = new Date(System.currentTimeMillis());
		category.setCreateTime(date);
		SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");		
		category.setCreateTimeString(sdf.format(date));
		
		String current_user_id = (String) this.getRequest().getSession().getAttribute("userId");
		category.setCreator(StaticMap.getUserValue(current_user_id));
		//设置CA_TYPE
		category.setCaTypes(SysConstant.CATEGORY_TYPE_SOFTWARE_MANAGEMENT);
		try{
		categoryManager.save(category);
		}catch(Exception e){
			e.printStackTrace();
		}
		if (returnUrl != null) {
			try {
				this.getResponse().sendRedirect(returnUrl);
			} catch (IOException e) {
				// TODO Auto-generated catch block
			}
			return returnUrl;
		} else {
			return LIST_ACTION;
		}
	}

	/** 进入更新页面 */
	public String edit() {
		return EDIT_JSP;
	}

	/** 保存更新对象 */
	public String update() {
		categoryManager.update(this.category);

		if (returnUrl != null) {
			try {
				this.getResponse().sendRedirect(returnUrl);
			} catch (IOException e) {
				// TODO Auto-generated catch block
			}
			return returnUrl;
		} else {
			return LIST_ACTION;
		}
	}
	public String showCategoryTree() {
		return "/admin/files/category/categorytree.jsp";
	}
	/**
     获得类型(FileTypes)为:SOFT的分类列表
	 */
	public String getCategoryList() {		
		String current_user_id = (String) this.getRequest().getSession().getAttribute("userId");
        List rt = new ArrayList();
		List list = (List) categoryManager.findByType(SysConstant.CATEGORY_TYPE_SOFTWARE_MANAGEMENT);
		if (list != null) {
			for (int i = 0; i < list.size(); i++) {
				Category ct = (Category) list.get(i);
	            Map map = new HashMap();
				map.put("id", ct.getId());
				map.put("pId", ct.getParentId());
				map.put("name", ct.getNames());
				map.put("open", "true");
				rt.add(map);
			}
		}
		return writeAjaxResponse(Json.toJson(rt));

	}

	/** 删除对象 */
	public String delete() {
		if (items != null)
			for (String ids : items) {
				String[] idArray = ids.split(",");
				for (String id : idArray)
					categoryManager.removeById(id);
			}
		return LIST_ACTION;
	}

	/** 删除对象 */
	public String remove() {
		
		categoryManager.removeById(id);
		
		if (returnUrl != null) {
			try {
				this.getResponse().sendRedirect(returnUrl);
			} catch (IOException e) {
				// TODO Auto-generated catch block
			}
			return returnUrl;
		} else {
			return LIST_ACTION;
		}
	}

	public String getReturnUrl() {
		return returnUrl;
	}

	public void setReturnUrl(String returnUrl) {
		this.returnUrl = returnUrl;
	}

}
